package com.xgr.wonderful.activity;

import java.util.ArrayList;
import java.util.List;

import com.viewpagerindicator.CirclePageIndicator;
import com.xgr.wonderful.activity.base.BaseActivity;
import com.xgr.wonderful.R;
import com.xgr.wonderful.utils.LogUtils;

import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.os.Handler;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.graphics.BitmapFactory;

/**
 * 目前 还有一点点的问题
 * @author lsj
 *
 */
public class FirstSetupActivity extends BaseActivity {
	private ViewPager mViewPager;
	private List<View> mViewList = new ArrayList<View>();
	private CirclePageIndicator mViewPagerIndicator;

	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(android.os.Message msg) {
			if (msg.what==1){
				//原来 为splashactivity
				redictToActivity(mContext, WelcomeActivity.class, null) ;
				finish() ;
			}
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
	    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.activity_first_setup);
		LogUtils.i(TAG, "FirstSetup") ;
		// 是否第一次启动应用,此处是用的版本号来判断的
		if (sputil.isFirstStart(mContext)) {
			LogUtils.i(TAG, "是第一次启动") ;
			sputil.setStarted(mContext);
			initViewPagers();
		}else {
			LogUtils.i(TAG, "不是第一次启动") ;
			redictToActivity(mContext, SplashActivity.class, null);
			finish() ;
		}
		
	}

	private void initViewPagers() {
		ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(
				ViewGroup.LayoutParams.MATCH_PARENT,
				ViewGroup.LayoutParams.MATCH_PARENT);

		for (int i = 0; i < 4; i++) {
			ImageView imageView = new ImageView(this);
			imageView.setLayoutParams(layoutParams);

			switch (i) {
			case 0:
				imageView.setImageBitmap(BitmapFactory.decodeResource(
						getResources(), R.drawable.welcome0));
				break;
			case 1:
				imageView.setImageBitmap(BitmapFactory.decodeResource(
						getResources(), R.drawable.welcome1));
				break;
			case 2:
				imageView.setImageBitmap(BitmapFactory.decodeResource(
						getResources(), R.drawable.welcome2));
				break;
			case 3:
				imageView.setImageBitmap(BitmapFactory.decodeResource(
						getResources(), R.drawable.welcome3));
				break;
			}
			imageView.setScaleType(ImageView.ScaleType.FIT_XY);
			mViewList.add(imageView);
		}
		mViewPager = (ViewPager) findViewById(R.id.viewpager);
		mViewPagerIndicator = (CirclePageIndicator) findViewById(R.id.indicator);
		mViewPager.setAdapter(new AdPagerAdapter(mViewList));
		mViewPagerIndicator.setViewPager(mViewPager);

		mViewPagerIndicator.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
				if (arg0 == 3) {
					mHandler.sendEmptyMessageDelayed(1, 1000);
				}

			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
			}
		});
	}

	private class AdPagerAdapter extends PagerAdapter {

		private List<View> viewList;

		public AdPagerAdapter(List<View> viewList) {
			this.viewList = viewList;
		}

		@Override
		public int getCount() {
			return viewList.size();
		}

		@Override
		public boolean isViewFromObject(View view, Object o) {
			return view == o;
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			container.addView(viewList.get(position));
			return viewList.get(position);
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView(viewList.get(position));
			object = null;
		}
	}

}
